package org.come.tt;

import com.tool.btn.FormsOnOffBtn;
import com.tool.image.ImageMixDeal;
import com.tool.role.RoleData;
import com.tool.tcp.Sprite;
import com.tool.tcp.SpriteFactory;
import com.tool.tcpimg.UIUtils;
import org.come.bean.QuackGameBean;
import org.come.entity.Goodstable;
import org.come.socket.Agreement;
import org.come.socket.SendMessageUntil;
import org.come.until.GoodsListFromServerUntil;
import org.come.until.UserMessUntil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseListener;
import java.math.BigDecimal;



public class LadderLotteryJpanel extends JPanel {

    // 背景图片
    private ImageIcon beijing;
    private JLabel[] prizeLab;
    private LadderLotteryBtn oneLotteryBtn, // 单次抽奖按钮
            tenLotteryBtn; // 十连抽抽奖按钮

    private String[] drawGoods; // 抽奖信息

    private JLabel integralLab;
    private JLabel consumePoints;

    private Boolean lotteryMarker = true;
    private int[] drawLotteryType;
    private static Sprite tcp = SpriteFactory.VloadSprite("resource/mouse/flicker.tcp", null);

    private int time = 0;

    private Integer a = null;
    private Integer b = 30;

    public JLabel getIntegralLab() {
        if (integralLab == null) {
            integralLab = new JLabel(RoleData.getRoleData().getLoginResult().getScoretype("天梯积分").toString());
            getIntegralLab().setBounds(360, 170, 260, 17);
            integralLab.setFont(UIUtils.TEXT_FONT6);
            integralLab.setOpaque(false);
            //integralLab.setForeground(UIUtils.COLOR_Draw1);
            integralLab.setForeground(UIUtils.COLOR_NAME3);
            this.add(integralLab);
        }
        return integralLab;
    }

    public int[] getDrawLotteryType() {
        if (this.drawLotteryType == null) {
            this.drawLotteryType = new int[16];

            for (int i = 0; i < this.drawLotteryType.length; ++i) {
                this.drawLotteryType[i] = 0;
                if (i == 5) {
                    this.drawLotteryType[i] = 1;
                }
            }
        }

        return this.drawLotteryType;
    }


    public JLabel getConsumePoints() {
        if (consumePoints == null) {
            consumePoints = new JLabel();
            getConsumePoints().setBounds(360, 198, 260, 17);
            consumePoints.setFont(UIUtils.TEXT_FONT6);
            consumePoints.setOpaque(false);
            //integralLab.setForeground(UIUtils.COLOR_Draw1);
            consumePoints.setForeground(UIUtils.COLOR_NAME3);
            this.add(consumePoints);
        }
        return consumePoints;
    }

    public LadderLotteryJpanel() {

        this.setPreferredSize(new Dimension(652, 446));
        this.setLayout(null);
        this.setBackground(UIUtils.Color_BACK);
        beijing = new ImageIcon("inkImg/old/background/tt-cj.png");
        getIntegralLab();
        getOneLotteryBtn();
        getPrizeLab();
        getConsumePoints();
        this.getDrawLotteryType();


        // 关闭按钮事件
        FormsOnOffBtn offBtn = new FormsOnOffBtn("inkImg/old/2/1.png", 1, 606);
        offBtn.setBounds(684 - 37, 10, 25, 25);
        this.add(offBtn);
    }

    // 发送切换面板信息给服务器
    public void getlotteryTypeGoods(int type) {
        String sendMes = Agreement.getFiveMsgAgreement("CC" + type);
        SendMessageUntil.toServer(sendMes);

    }

    public String[] getDrawGoods() {
        return drawGoods;
    }

    public void getMenuImg(QuackGameBean gameBean) {
        {
            if (gameBean.getType() == 7)
                return;
            String menuType = gameBean.getPetmsg();
            String[] split = menuType.split("\\|");
            JLabel consumePoints = getConsumePoints();
            consumePoints.setText(gameBean.getMoney().toString());
            int j = 0;
            for (int i = 0; i < split.length - 1; i++) {
                if (split[i].startsWith("Y")) continue;
                else if (split[i].startsWith("N")) continue;
                else {
                    Goodstable good = UserMessUntil.getgoodstable(new BigDecimal(split[i]));
                    if (good != null) {
                        this.getPrizeLab()[j].setIcon(GoodsListFromServerUntil.imgpathAdaptive(good.getSkin(), 49, 49));
                        MouseListener[] mouseListeners = this.getPrizeLab()[j].getMouseListeners();
                        org.lottery.mouseListener.PrizeMouseListener listener = (org.lottery.mouseListener.PrizeMouseListener) mouseListeners[0];
                        listener.setGoodsId(split[i]);
                        j++;
                    }
                }

            }
        }

//        Goodstable good = goodid != null ? UserMessUntil.getgoodstable(goodid) : null;
//        if (good != null) {
//            this.getConsumeGoodsImg().setIcon(GoodsListFromServerUntil.imgpathAdaptive(good.getSkin(), 49, 49));
//            int num = 0;
//            for (int k = 0; k < GoodsListFromServerUntil.getGoodslist().length; k++) {
//                Goodstable goodstable = GoodsListFromServerUntil.getGoodslist()[k];
//                if (goodstable != null && goodstable.getGoodsid().compareTo(goodid) == 0) {
//                    num += goodstable.getUsetime();
//                }
//            }
//            this.getConsumeGoodsNumber().setText(num + "");
//        } else {
//            this.getConsumeGoodsImg().setIcon(null);
//            this.getConsumeGoodsNumber().setText("");
//        }
    }

    public LadderLotteryBtn getOneLotteryBtn() {
        if (oneLotteryBtn == null) {
            oneLotteryBtn = new LadderLotteryBtn("skin/8100226/newbutton/B20.png", 1, "抽奖", 10, this);
            oneLotteryBtn.setBounds(291, 393, 59, 24);
            this.add(oneLotteryBtn);
        }
        return oneLotteryBtn;
    }

    public JLabel[] getPrizeLab() {
        if (prizeLab == null) {
            prizeLab = new JLabel[16];

            for (int i = 0; i < prizeLab.length; i++) {
                prizeLab[i] = new JLabel();
//            prizeLab[i].setBounds(i * 30 , 55, 50, 50);
                prizeLab[i].setBounds(i % 6 * 95 + 65, 55, 50, 50);
                if (i >= 6 && i <= 7)
                    prizeLab[i].setBounds(i % 2 * 470 + 65, 148, 50, 50);
                if (i >= 8 && i <= 9)
                    prizeLab[i].setBounds(i % 2 * 470 + 65, 238, 50, 50);
                if (i >= 10)
                    prizeLab[i].setBounds(i % 6 * 95 + 65, 330, 50, 50);

                prizeLab[i].addMouseListener(new org.lottery.mouseListener.PrizeMouseListener(null));
                prizeLab[i].setOpaque(false);
                this.add(prizeLab[i]);
            }

        }
        return prizeLab;
    }

    @Override
    protected void paintComponent(Graphics g) {
        // TODO Auto-generated method stub
        super.paintComponent(g);
        g.drawImage(beijing.getImage(), 0, 0, 652, 446, null);
        this.integralLab.setText(RoleData.getRoleData().getLoginResult().getScoretype("天梯积分").toString());
        tcp.updateToTime(ImageMixDeal.userimg.getTime(), 0);
        for (JLabel jLabel : this.prizeLab) {
            tcp.draw(g, (int) jLabel.getBounds().getX() - 1, (int) jLabel.getBounds().getY() - 1);
        }
//        if (lotteryMarker) {
//            if (a == null)
//                a = 0;
//            Rectangle bounds = prizeLab[a].getBounds();
//            tcp.updateToTime(ImageMixDeal.userimg.getTime(), 0);
//            tcp.draw(g, (int) bounds.getX() - 3, (int) bounds.getY() - 1);
//            if (time > b) {
////            a += 1;
////            if (a > prizeLab.length - 1)
////                a = 0;
//                a = new Random().nextInt(prizeLab.length);
//                time = 0;
//                if (b == 0)
//                    b = 30;
//                b -= 1;
//            }
//            ++this.time;
//        }

    }


}


